﻿using UnityEngine;
using System.Collections;
using Dict.DataModel;
using ProtoBuf.Message;
using System.Collections.Generic;

namespace UI
{
    
    public class PetFeedTab : MonoBehaviour
    {
        public enum HUDONGTYPE
        {
            None,
            Eat,
            Play,
            Bath,
            Touch,
        }

        private HUDONGTYPE hudongType = HUDONGTYPE.None;
        public int curlntimacylevel = 0;//当前亲密等级
        #region 宠物信息

        /// <summary>
        /// 亲密等级心
        /// </summary>
        public List<GameObject> lntimacylevel; 

        /// 亲密经验Lab
        public UILabel needlntimacyValue;//亲密值

        /// 经验条spr
        public UISprite expBar;

       ///经验增加spr
        public UISprite expAddBar;

        public UILabel TipLab;
        public UILabel lntimacyLevLab;
        public UILabel lntimacyExpLab;

        ///解锁属性的预设
        public GameObject AtrrItemPrefab;
        public GameObject AtrrGrid;

        //喂食
        public GameObject LoveFodGrid;
        public GameObject UnLoveFodGrid;
        public GameObject fodPrefab;

        public GameObject FeedPage;
        public GameObject PetTanletInfo;
        public GameObject OperateInfo;
        public List<PetPropertyLab> Propertys;
        #endregion
        string petGuid;
        private List<GameObject> loveItem;
        private List<GameObject> unloveItem;
        private List<string> attrs; // attr,100
        UIPet petInfo;


        public void SetPet(string guid)
        {
            petGuid = guid;
            petInfo = StaticData.playerData.InstPlayerData.GetUIPet(petGuid);
            if (petInfo == null)
                return;
            loveItem = new List<GameObject>();
            unloveItem = new List<GameObject>();
            attrs = DictPetTalentBlo.GetPropInfo(petInfo.randPropList, petInfo.breachlevel);
            UpdatePetlntimacyInfo();
           
            PropertyInfo();
        }

        #region 倒计时


        #endregion

        #region 刷新界面
        void UpdatePetlntimacyInfo()
        {
            UIPet pet = StaticData.playerData.GetUIPet(petGuid);

            for (int i = 0; i < pet.intimacyLevel; ++i)
            {
                lntimacylevel[i].SetActive(true);
            }
            UpDateProgessValue();
        }

        void UpDateProgessValue()
        {            

        }

        void CreateLoveAndUnloveItem()
        {
            //clear
            for (int i = 0; i < loveItem.Count; i++)
            {
                LoveFodGrid.transform.DestroyChildren();
                loveItem.Clear();
            }
            for (int i = 0; i < unloveItem.Count; i++)
            {
                UnLoveFodGrid.transform.DestroyChildren();
                unloveItem.Clear();
            }

            List<string> unlove;
            List<string> love = Dict.Blo.DictPetBlo.GetPetLoveAndUnLoveItem(petInfo.petData.nature,out unlove);
            foreach(string itemid in love)
            {
                loveItem.Add(CreatItem(LoveFodGrid, itemid));
            }
            LoveFodGrid.GetComponent<UIGrid>().repositionNow = true;
            foreach (string itemid in unlove)
            {
              unloveItem.Add(CreatItem(UnLoveFodGrid, itemid));
            }
            UnLoveFodGrid.GetComponent<UIGrid>().repositionNow = true;
        }

        GameObject CreatItem(GameObject grid,string itemID)
        {
            
            GameObject obj = AssetLoad.ResourceManager.Instance.LoadResource("prefab/ItemIcon") as GameObject;
            GameObject itemIcon = Instantiate(obj);
            ItemIcon itemScript = itemIcon.GetComponent<ItemIcon>();
            itemScript.SetItem(itemID);
            itemScript.UpdateItemCount(StaticData.playerData.GetSimpleGoods(itemID).num.ToString(), UIWidget.Pivot.Right);

            itemIcon.AddComponent<UIDragScrollView>().scrollView = grid.transform.parent.GetComponent<UIScrollView>();

            itemScript.m_callback = OnFodItemClick;
            itemIcon.transform.parent = grid.transform;
            itemIcon.transform.localPosition = new Vector3(0, 0, 0);
            itemIcon.transform.localScale = new Vector3(0.7f, 0.7f, 0.7f);
            return itemIcon;
        }
        void OnFodItemClick(string itemid)
        {
            List<BaseResource> br = new List<BaseResource>();
            BaseResource baseSour = new BaseResource();
            baseSour.num = 1;
            baseSour.id = itemid;
            // baseSour.guid=StaticData.playerData.GetSimpleGoods(itemid).
            CSPetEat msg = new CSPetEat() {
               
            };
            msg.guid = petGuid;
            msg.cost.Add(baseSour);

            NetAPI.SendHttp(OpDefine.CSPetEat, msg, (packet, bSuccess) =>
            {
                if (bSuccess)
                {
                    SCPetEat _msg = packet.kBody as SCPetEat;
                    StaticData.playerData.UpdatePet(_msg.pet);
                    UpdatePetlntimacyInfo();
                    CreateLoveAndUnloveItem();
                }
            });
        }


        //更新解锁的属性信息
        void PropertyInfo()
        {
            int lockCount = Dict.Blo.DictPetintimacyBlo.GetAddtionTalentByIntimacyLevelAndQuality(petInfo.intimacyLevel, petInfo.petData.quality);
            for(int i=0,max=attrs.Count;i< max;i++)
            {
                string[] strs = attrs[i].Split(',');
                if(i<lockCount-1)//解锁
                    CreateAttrLab(strs[0],i, int.Parse(strs[1]));
                else
                {
                    CreateAttrLab(strs[0],i, int.Parse(strs[1]),i);
                }
            }
        }
        void CreateAttrLab(string name,int index,int value ,int level=-1)
        {
            Propertys[index].SetValue(name, value, level);
        }
        #endregion

        #region Click
        public void OnClickCloseFeedBtn()
        {
            PetTanletInfo.SetActive(true);
            OperateInfo.SetActive(true);
            FeedPage.SetActive(false);
        }
        //喂食
        public void OnClickFeedBtn()
        {
            PetTanletInfo.SetActive(false);
            OperateInfo.SetActive(false);
            FeedPage.SetActive(true);
            
        }


        //抚摸
        public void OnClickTouchBtn()
        {
            hudongType = HUDONGTYPE.Touch;
            Debug.Log("OnClickTouchBtn");
            PetHuDong(1);
        }

        //洗澡
        public void OnClickBatheBtn()
        {
            hudongType = HUDONGTYPE.Bath;
            Debug.Log("OnClickTouchBtn");
            PetHuDong(0);
        }

        //玩耍
       public void OnClickPlayBtn()
        {
            hudongType = HUDONGTYPE.Play;
            Debug.Log("OnClickPlayBtn");
            PetHuDong(2);
        }

        //互动消息发送
         void PetHuDong(int type)
        {
            CSPetHudong msg = new CSPetHudong();
            msg.type = type;
            msg.guid = petGuid;
            NetAPI.SendHttp(OpDefine.CSPetHudong, msg, (packet, bSuccess) =>
            {
                if (bSuccess)
                {
                    SCPetHudong _msg = packet.kBody as SCPetHudong;
                    StaticData.playerData.UpdatePet(_msg.pet);
                    UpdatePetlntimacyInfo();
                    PlayHudongAni(hudongType);
                }
            });
        }

        //播放互动效果
        private void PlayHudongAni(HUDONGTYPE type)
        {
            switch (type)
            {
                case HUDONGTYPE.None:
                    break;
                case HUDONGTYPE.Eat:
                    break;
                case HUDONGTYPE.Play:
                    break;
                case HUDONGTYPE.Bath:
                    break;
                case HUDONGTYPE.Touch:
                    break;
                default:
                    break;
            }
        }
        #endregion
    }
}

